package cn.edu.nwafu.hjpg.controller;


import cn.edu.nwafu.hjpg.common.HttpResult;
import cn.edu.nwafu.hjpg.entity.*;
import cn.edu.nwafu.hjpg.service.LhsxbService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/lhsxb")
public class LhsxbController {
    final LhsxbService lhsxbService;
    //private boolean required;

    public LhsxbController(LhsxbService lhsxbService) {
        this.lhsxbService = lhsxbService;
    }

    @GetMapping(value = "/")
    @ResponseBody
    public HttpResult get(@RequestParam(value = "labelName",defaultValue = "",required = false) String name,
                          @RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                          @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        return HttpResult.ok(lhsxbService.findByIdAndFlagInLhsxb(id,name,flag));
    }
    @GetMapping(value = "/export")
    @ResponseBody
    public HttpResult get() {
        return HttpResult.ok(lhsxbService.findAll());
    }
    @PostMapping(value = "/update")
    @ResponseBody
    public HttpResult update(@RequestBody Lhsxb lhsxb) {
        return HttpResult.ok(lhsxbService.updateLhsxb(lhsxb));
    }

    @PostMapping(value = "/add")
    @ResponseBody
    public HttpResult add(@RequestBody(required = false) Lhsxb lhsxb,@RequestParam(value = "labelName",defaultValue = "",required = false) String name) {
        switch(name){
            case "基本性状":
                return HttpResult.ok(lhsxbService.addJbxz(lhsxb));
            case "沸点":
                return HttpResult.ok(lhsxbService.addFd(lhsxb));
            case "水溶解度":
                return HttpResult.ok(lhsxbService.addSrjd(lhsxb));
            case "饱和蒸气压":
                return HttpResult.ok(lhsxbService.addBhzqy(lhsxb));
            case "熔点":
                return HttpResult.ok(lhsxbService.addRd(lhsxb));
            case "相对密度":
                return HttpResult.ok(lhsxbService.addXdmd(lhsxb));
            case "闪点":
                return HttpResult.ok(lhsxbService.addSd(lhsxb));
            case "亨利常数":
                return HttpResult.ok(lhsxbService.addHlcs(lhsxb));
            case "Log Kow":
                return HttpResult.ok(lhsxbService.addLogkow(lhsxb));
            case "Koc":
                return HttpResult.ok(lhsxbService.addKoc(lhsxb));
            case "解离常数":
                return HttpResult.ok(lhsxbService.addJlcs(lhsxb));
            case "燃烧性":
                return HttpResult.ok(lhsxbService.addRsx(lhsxb));
            case "氧化性":
                return HttpResult.ok(lhsxbService.addYhx(lhsxb));
            case "爆炸性":
                return HttpResult.ok(lhsxbService.addBzx(lhsxb));
            case "表面张力":
                return HttpResult.ok(lhsxbService.addBmzl(lhsxb));
            case "PH":
                return HttpResult.ok(lhsxbService.addPh(lhsxb));
            default:
                return null;
        }
    }
    @GetMapping(value = "/addRecord")
    @ResponseBody
    public HttpResult queryLhsxbByPage(@RequestParam(value = "flag", defaultValue = "") String flag,
                                      @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                      @RequestParam(value = "pageSize", defaultValue = "20") int pageSize){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(lhsxbService.findAddRecordLhsxb(flag,pageable));
    }
    @GetMapping(value = "/find")
    @ResponseBody
    public HttpResult findLhsxbByPage(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                       @RequestParam(value = "pageSize", defaultValue = "7") int pageSize,
                                       @RequestParam(value = "attribute", defaultValue = "") String attribute,
                                       @RequestParam(value = "flag", defaultValue = "1") int flag,
                                      @RequestParam(value = "id",defaultValue = "",required = false) String id){

        Pageable pageable = PageRequest.of(pageNumber - 1, pageSize);
        return HttpResult.ok(lhsxbService.findByPageLhsxb(id,attribute,flag,pageable));
    }

    @PostMapping(value = "/addupdateFlag")
    @ResponseBody
    public HttpResult addupdateFlag(@RequestBody Lhsxb lhsxb) {
        System.out.println(lhsxb.toString());
        return HttpResult.ok(lhsxbService.addupdateFlag(lhsxb));
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public HttpResult deladdRecordlhsxb(Lhsxb lhsxb){
        lhsxbService.deladdRecordlhsxb(lhsxb);
        return HttpResult.ok();
    }
    @PostMapping(value = "/import")
    @ResponseBody
    public HttpResult addExcelInLhsxb(@RequestBody List<Lhsxb> lhsxbList) {
        for (int i = 0 ; i <lhsxbList.size() ; i ++ ){
//            lhsxbList.get(i).setFlag("1");
            lhsxbService.addElement(lhsxbList.get(i));

        }
        return HttpResult.ok();
    }

    @GetMapping(value = "/all")
    @ResponseBody
    public HttpResult findAll(@RequestParam(value = "elementId",defaultValue = "",required = false) String id,
                              @RequestParam(value = "flag",defaultValue = "",required = false) String flag) {
        System.out.println("**********************");
        return HttpResult.ok(lhsxbService.findAllByBaseinfoWzbmAndFlag(id,flag));
    }
}

